package com.sh.stream流;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class StreamDemo7 {
    public static void main(String[] args) {
        ArrayList<String> nameList = new ArrayList<>();
        Collections.addAll(nameList, "张无忌-男-11", "张三-女-22", "周芷若-男-33",  "孙悟空-男-44", "猪八戒-女-55");
//        nameList.stream().forEach(System.out::println);

//        System.out.println(nameList.stream().count());


        /*String[] array = nameList.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });
        for (String s : array) {
            System.out.println(s);
        }*/


//        String[] array = nameList.stream().toArray(value -> new String[value]);
//        System.out.println(Arrays.toString(array));
//        nameList.stream().filter(name -> "男".equals(name.split("-")[1])).forEach(System.out::println);

        // **收集***
        // 1.list中
//        List<String> collect = nameList.stream()
//                .filter(name -> "男".equals(name.split("-")[1]))
//                .collect(Collectors.toList());
//        System.out.println(Arrays.toString(collect.toArray()));
        // 2.Set中
//        Set<String> collect = nameList.stream()
//                .filter(name -> "男".equals(name.split("-")[1]))
//                .collect(Collectors.toSet());
//        System.out.println(Arrays.toString(collect.toArray()));

        // 3.Map中
        Map<String, Integer> collect = nameList.stream()
                .filter(name -> "男".equals(name.split("-")[1]))
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split("-")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        String s1 = s.split("-")[2];
                        return Integer.valueOf(s1);
                    }
                }));
        System.out.println(collect);

        Map<String, Integer> collect2 = nameList.stream()
                .filter(name -> "男".equals(name.split("-")[1]))
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> Integer.valueOf(s.split("-")[2])));
        System.out.println(collect2);


    }
}

//